package com.lzy.cloud.order.facade;

import com.alibaba.fastjson.JSON;
import com.lzy.cloud.order.constants.CommonConstants;
import com.lzy.cloud.order.constants.OrderDistributePrefix;
import com.lzy.cloud.order.constants.OrderStatusDesEnum;
import com.lzy.cloud.order.dos.OrderActionDO;
import com.lzy.cloud.order.dtos.pay.OrderPayDto;
import com.lzy.cloud.order.dtos.pay.OrderPayParamsDto;
import com.lzy.cloud.order.service.OrderInfoDetailService;
import com.lzy.cloud.order.service.OrderInfoService;
import com.lzy.cloud.order.service.OrderRefundLogService;
import com.lzy.framework.core.cache.cluster.RedisClusterService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 订单-支付
 * @Author:   gyl
 * @Date:     2018/7/10 11:32
 */
@Component
@Slf4j
public class OrderPayFacade {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderPayFacade.class);

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private OrderInfoDetailService orderInfoDetailService;

    @Autowired
    private OrderRefundLogService orderRefundLogService;

    @Autowired
    private RedisClusterService redisClusterService;

    /**
     * 方法说明：根据订单号查询订单支付信息
     * @param   orderId
     * @return
     * @date        2018/7/10 11:24
     */
    public OrderPayDto findToPay(String orderId){
        OrderPayDto orderPayDto = orderInfoService.findToPay(orderId);
        return orderPayDto;
    }

    /**
     * 方法说明：根据订单号更新相关信息
     * @param orderPayParamsDto
     * @return      
     * @date        2018/7/10 11:15
     */
    public Boolean updateFromPay(OrderPayParamsDto orderPayParamsDto) {
        Ensure.that(orderPayParamsDto).isNotNull("");
        return orderInfoService.updateFromPay(orderPayParamsDto);
    }

    /**
     * 方法说明：退款更新相关信息
     * @param orderPayParamsDto
     * @return      
     * @date        2018/7/10 11:31
     */
    public Boolean updateFromRefund(OrderPayParamsDto orderPayParamsDto) {
        Ensure.that(orderPayParamsDto).isNotNull("");
        OrderActionDO oaDO = new OrderActionDO();
        oaDO.setOrderId(orderPayParamsDto.getOrderId());
        oaDO.setMaxStatus(CommonConstants.OrderBigStatus.BACK_ORDER);
        oaDO.setOsde(OrderStatusDesEnum.CAMPSITE_PAYED);
        if (orderInfoService.updateStatus(oaDO)) {
            String key = OrderDistributePrefix.QueueOrder.QUEUE_ORDER_MSG + orderPayParamsDto.getOrderId();
            String flag = redisClusterService.get(key, String.class);
            // 判断有没有执行过
            if (StringUtil.isNotBlank(flag) && CommonConstants.ActionFlag.NO.toString().equals(flag)) {
                // 订单已经分利，打印信息
                LOGGER.error("退款成功短信，不能重复发送: " + JSON.toJSONString(oaDO));
            } else {
                // 发送短信-退款成功
//                OrderMessageDTO dto = new OrderMessageDTO();
//                dto.setOrderId(orderPayParamsDto.getOrderId());
//                dto.setMinStatus(OrderSatusDesEnum.CAMPSITE_PAYED.getStatus());
//                orderMsgApi.sendMessage(dto);
//                //kvClient.addQueue(OrderDistributePrefix.QueueOrder.QUEUE_ORDER_MESSAGE, dto);
//                // 分利标识
//                kvClient.save(key, CommonConstants.ActionFlag.NO, EXPIRED_SECOND);
            }
            return orderRefundLogService.updateByOrderId(orderPayParamsDto);
        }
        return Boolean.FALSE;
    }

}
